﻿using _2008A.PH5._0221_0317.FuXi.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace _2008A.PH5._0221_0317.FuXi.API.Controllers
{
    [Route("product")]
    [ApiController]
    public class ProductController : ControllerBase
    {

        ICategoryService categoryService;
        IBrandService _brandService;
        IProductService _productService;
        public ProductController(ICategoryService categoryService, IBrandService brandService, IProductService productService)
        {
            this.categoryService = categoryService;
            this._brandService = brandService;
            this._productService = productService;
        }

        /// <summary>
        /// 商品列表初始化（三套合一）
        /// </summary>
        /// <param name="categoryId">分类</param>
        /// <param name="brandId">品牌</param>
        /// <param name="minPrice">最小价格</param>
        /// <param name="maxPrice">最大价格</param>
        /// <param name="sortFiled">排序字段</param>
        /// <param name="isDesc">排序值（升还是降）</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index(string? categoryId, string? brandId,
            decimal? minPrice, decimal? maxPrice,
            string? sortFiled="sale", bool isDesc = true,
            int pageIndex = 1, int pageSize = 10)
        {
            var categoryData = categoryService.GetRecursionCategory();//分类的数据
            var brandData = this._brandService.Query().ToList();  //品牌数据

            //所有的产品商品数据
            var _productList = _productService.Query().ToList();

            var hotSaleData = _productList.OrderByDescending(x => x.SaleNumber).Take(4); //热销的商品数据（4条）

            #region //产品商品数据，功能有：排序、查询、分页

            //分类、品牌查询
            var productData = _productList.Where(x => x.CategoryId.ToString().Contains(categoryId ?? ""))
                                          .Where(x => x.BrandId.ToString().Contains(brandId ?? ""));
            //价格查询
            if (minPrice <= 0)
                minPrice = 0;

            if (minPrice >= 0 && minPrice < maxPrice)
                productData = productData.Where(x => x.Price >= minPrice && x.Price <= maxPrice);

            //排序
            switch (sortFiled.ToLower())
            {
                case "sale":
                    if (!isDesc)
                    {
                        productData = productData.OrderBy(x => x.SaleNumber);
                    }
                    else
                    {
                        productData = productData.OrderByDescending(x => x.SaleNumber);
                    }
                    break;
                case "price":
                    if (!isDesc)
                    {
                        productData = productData.OrderBy(x => x.Price);
                    }
                    else
                    {
                        productData = productData.OrderByDescending(x => x.Price);
                    }
                    break;
            }

            //分页
            int total = productData.Count();
            productData = productData.Skip((pageIndex - 1) * pageSize).Take(pageSize);

            #endregion


            return Ok(new
            {
                code = 200,
                message = "Success",
                data = new
                {
                    categoryData,
                    brandData,
                    productData,
                    hotSaleData,
                    total
                }
            });
        }
    }
}
